package com.yuyou.activity.group.service.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuyou.activity.group.service.entity.ActivityGroupbuyUser;
import com.yuyou.activity.group.service.lock.MapLock;
import com.yuyou.activity.group.service.mapper.ActivityGroupbuyUserMapper;
import com.yuyou.activity.group.service.service.IActivityGroupbuyUserService;
import com.yuyou.activity.util.bean.ResBoolSimpleInfo;
import com.yuyou.activity.util.exception.BusinessException;
import com.yuyou.activity.util.util.Sequence;

@Service
public class ActivityGroupbuyUserServiceImpl implements IActivityGroupbuyUserService {

    @Autowired
    private ActivityGroupbuyUserMapper mapper;

    @Override
    public int deleteByPrimaryKey(Long activityGroupbuyUserId) {
        return mapper.deleteByPrimaryKey(activityGroupbuyUserId);
    }

    @Override
    public int insert(ActivityGroupbuyUser record) {
    	record.setActivityGroupbuyUserId(Sequence.getSequence().nextId());
    	record.setCreateTime(new Date());
        return mapper.insert(record);
    }

    @Override
    public ActivityGroupbuyUser selectByPrimaryKey(Long activityGroupbuyUserId) {
        return mapper.selectByPrimaryKey(activityGroupbuyUserId);
    }

    @Override
    public int updateByPrimaryKeySelective(ActivityGroupbuyUser record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

	@Override
	public ResBoolSimpleInfo<String> add(ActivityGroupbuyUser record) {
		this.insert(record);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public ActivityGroupbuyUser selectByOpenId(String projectCode, String openId) {
		return mapper.selectByOpenId(projectCode, openId);
	}
	
	@Override
	@Transactional
	public ActivityGroupbuyUser addOrUpUser(String openId,String headImg,String name,String projectCode) throws Exception {		
		if(StringUtils.isEmpty(openId)|| StringUtils.isEmpty(projectCode)){
			throw new BusinessException("openId,projectCode不能为空");
		}
		try{
		boolean lockRes=MapLock.trylock("sp"+projectCode+openId);
		if(lockRes){
			ActivityGroupbuyUser user=addOrUpUserLock(openId,headImg,name,projectCode);
			return user;
		}
		}finally{
			MapLock.removeLock("sp"+projectCode+openId);
		}
		return null;
		
	}
	
	public ActivityGroupbuyUser addOrUpUserLock(String openId,String headImg,String name,String projectCode) {	
		ActivityGroupbuyUser activityGroupbuyUser=selectByOpenId(projectCode,openId);
		if(activityGroupbuyUser==null){
			activityGroupbuyUser=new ActivityGroupbuyUser();
			activityGroupbuyUser.setOpenId(openId);
			activityGroupbuyUser.setName(name);
			activityGroupbuyUser.setHeadImg(headImg);
			activityGroupbuyUser.setProjectCode(projectCode);
			this.insert(activityGroupbuyUser);
			return activityGroupbuyUser;
		}else{
			if(StringUtils.isNotEmpty(name)&&!name.equals(activityGroupbuyUser.getName())){
				activityGroupbuyUser.setName(name);
				this.updateByPrimaryKeySelective(activityGroupbuyUser);
			}
			if(StringUtils.isNotEmpty(headImg)&&!headImg.equals(activityGroupbuyUser.getHeadImg())){
				activityGroupbuyUser.setHeadImg(headImg);
				this.updateByPrimaryKeySelective(activityGroupbuyUser);
			}
			return activityGroupbuyUser;
		}
		
	}
	
}